package com.mvvmhabit.org.binding.viewadapter.labellist

import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableArrayList
import com.mvvmhabit.org.binding.command.BindingCommand
import com.mvvmhabit.org.custom.LabelListView
import com.mvvmhabit.org.custom.SpinnerItemData


@BindingAdapter(value = ["labels", "onLabelSelectedCommand"], requireAll = false)
fun onLabelSelectedCommand(
    labelListView: LabelListView,
    labels: ObservableArrayList<SpinnerItemData>,
    onLabelSelectedCommand: BindingCommand<SpinnerItemData>
) {
    if (labels.size > 0) {
        labelListView.setLabels(
            labels
        ) { _, _, data -> data.getKey() }
        labelListView.setOnLabelSelectChangeListener { _, _, _, position ->
            onLabelSelectedCommand.execute(labels[position])
        }
    }
}